package com.app.ytong.manager;

import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.ytong.R;
import com.app.ytong.basis.BasisActivity;
import com.app.ytong.basis.Constants;
import com.app.ytong.bean.BaseListBeanXF;
import com.app.ytong.bean.GoodsLevelListBean;
import com.app.ytong.bean.GoodsListBean;
import com.app.ytong.bean.GoodsListData;
import com.app.ytong.bean.GoodsTypeListBean;
import com.app.ytong.bean.LoginBean;
import com.app.ytong.bean.MenuBean;
import com.app.ytong.bean.RecordListBean;
import com.app.ytong.bean.RecordListData;
import com.app.ytong.greendao.GreenDaoUtils;
import com.app.ytong.manager.in.PutInActivity;
import com.app.ytong.manager.qing.ScaleActivity;
import com.app.ytong.record.RecordInfoActivity;
import com.app.ytong.record.RecordListActivity;
import com.app.ytong.util.MyUtils;
import com.jaredrummler.materialspinner.MaterialSpinner;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import my.ActivityTool;
import my.http.MyHttpListener;
import my.http.MyHttpUtils;
import my.http.MyParams;

/**
 * 入库管理-出入存
 */
public class StockActivity extends BasisActivity {

    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    private MyAdapterList mAdapter;
    GoodsListBean mListBean;
    GoodsTypeListBean goodsTypeListBean;
    GoodsLevelListBean goodsLevelListBean;

    @BindView(R.id.spinner_type)
    MaterialSpinner spinner_type;
    @BindView(R.id.spinner_level)
    MaterialSpinner spinner_level;
    @BindView(R.id.edit_key)
    EditText edit_key;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void initViews() {
        setContentView(R.layout.stock_list_activity);
        setTitle("库存查询");
        setTitleBg(R.color.base_blue);
        setTitleLeftButton(null);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mListBean = new GoodsListBean();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        QMUIStatusBarHelper.setStatusBarDarkMode(mContext);

        goodsTypeListBean = GreenDaoUtils.getGoodsTypeListBean();
        spinner_type.setItems(goodsTypeListBean.getAllName());
        spinner_type.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {

            @Override public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
                getList();
            }
        });
        goodsLevelListBean = GreenDaoUtils.getGoodsLevelListBean();
        spinner_level.setItems(goodsLevelListBean.getAllName());
        spinner_level.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {

            @Override public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
                getList();
            }
        });
        getList();
    }


    @OnClick({R.id.search_btn})
    public void btnClick(View v){
        switch (v.getId()){
            case R.id.search_btn:
                getList();
          
        }
    }


    private void getList() {
        MyParams params = new MyParams();
        showProgress();
        params.put("aid", LoginBean.getInstance().getAid());
        String key = edit_key.getText().toString().trim();
        if(!TextUtils.isEmpty(key)){
            params.put("gna", key);
        }
        params.put("tid", goodsTypeListBean.getTidByPosition(spinner_type.getSelectedIndex()));
        params.put("lid", goodsLevelListBean.getLidByPosition(spinner_level.getSelectedIndex()));
        params.put("ifs", MyUtils.getHomeSelectId());


        MyHttpUtils.get(Constants.URL_STOCK_SELECT_ALL, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        if (isFinishing()) return;
                        mListBean = (GoodsListBean) result;
                    }

                    @Override
                    public void onFailure(int httpWhat, Object result) {
                        if (isFinishing()) return;
                        mListBean.getList().clear();
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        if (isFinishing()) return;
                        dismissProgress();
                        setListView();
                    }
                },
                0, GoodsListBean.class);
    }

    private void setListView() {
        if (mAdapter == null) {
            mAdapter = new MyAdapterList();
            mRecyclerView.setAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
    }

    public class MyAdapterList extends RecyclerView.Adapter<MyAdapterList.ViewHolder> {
        public MyAdapterList() {

        }

        //创建新View，被LayoutManager所调用
        @Override
        public MyAdapterList.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.stock_list_item, viewGroup, false);
            return new MyAdapterList.ViewHolder(view);
        }

        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(MyAdapterList.ViewHolder viewHolder, int position) {
            viewHolder.bind(position);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            int count = mListBean == null ? 0 : mListBean.getList().size();
            return count;
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {

            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.text_no)
            public TextView text_no;
            @BindView(R.id.text_gna)
            public TextView text_gna;
            @BindView(R.id.text_gno)
            public TextView text_gno;
            @BindView(R.id.text_gsp)
            public TextView text_gsp;
            @BindView(R.id.text_gun)
            public TextView text_gun;
            @BindView(R.id.text_gnum)
            public TextView text_gnum;


            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            public void bind(int position) {
                GoodsListData data = mListBean.getList().get(position);
                text_no.setText(""+(position+1));
                MyUtils.setNotNullText(text_gna,""+data.getGna());
                MyUtils.setNotNullText(text_gno,""+data.getGno());
                MyUtils.setNotNullText(text_gsp,""+data.getGsp());
                MyUtils.setNotNullText(text_gun,""+data.getGun());
                MyUtils.setNotNullText(text_gnum,""+data.getGnum());
                list_item.setTag(data);
            }
        }
    }

    

}